from nuscenes.nuscenes import NuScenes

# nuscenes 存放路径
nuscenes_data = "E:\\data\\3DpointCloud\\nuscenes\\nuscenes_mini"
nusc = NuScenes(version="v1.0-mini", dataroot=nuscenes_data, verbose=True)

# 1 场景   （scence）
nusc.list_scenes()

my_scence = nusc.scene[0]   # 取出第一个场景
# print(my_scence)

# 2 样本  （场景里面包含多个样本） scenes -> sample
first_sample_token = my_scence["first_sample_token"]
my_sample = nusc.get('sample', first_sample_token)
# print(my_sample)

# 列出 my_sample 相关的关键帧
# nusc.list_sample(my_sample["token"])

# 3 样本数据(相关传感器信息) sample -> sample_data
my_sample["data"]
# print(my_sample["data"])
sensor = "CAM_FRONT"
cam_front_data = nusc.get("sample_data", my_sample["data"][sensor])
print(cam_front_data)
print("------")
# nusc.render_sample_data(cam_front_data["token"])   # 展示样本传感器的图片

# 4 样本标注  sample -> sample_annotation
my_annotation_token = my_sample['anns'][18]
my_annotation_metadata = nusc.get("sample_annotation", my_annotation_token)
# print(my_annotation_metadata)

# nusc.render_annotation(my_annotation_token, out_path='./annotation.png')

# 5 实例     sample_annotation <-> instance
my_instance = nusc.instance[599]
print(my_instance)
instance_token = my_instance['token']
nusc.render_instance(instance_token, out_path='./instance.png')

# 6 类别 instance -> category
# nusc.list_categories()

# 7 属性  instance -> attribute  (相同的实例在相同的场景下不同的属性)
nusc.list_attributes()

# 同一个实例在相同的场景下，有多少个属性

my_instance = nusc.instance[27]
first_token = my_instance["first_annotation_token"]
last_token = my_instance["last_annotation_token"]
nbr_samples = my_instance['nbr_annotations']
current_token = first_token

i = 0  # 首次运行
found_change = False
while current_token != last_token:
    current_ann = nusc.get("sample_annotation", current_token)
    current_arr = nusc.get("attribute", current_ann["attribute_tokens"][0])['name']
    # print(nusc.get("attribute", current_ann["attribute_tokens"][0]))

    if i == 0:
        pass
    elif current_arr != last_atrr:
        print("Changed from {} to {} at timestamp {} out of {} annoated timestamps".format(last_atrr, current_arr,i, nbr_samples))
        found_change = True

    next_token = current_ann["next"]
    current_token = next_token
    last_atrr = current_arr
    i += 1

# 8 可见度  visibility
print(nusc.visibility)

# 以可见度 80-100% 的 sample_annotation
ann_token = "a7d0722bce164f88adf03ada491ea0ba"
visibility_token = nusc.get("sample_annotation", ann_token)["visibility_token"]

print("Visibility: {}".format(nusc.get("visibility", visibility_token)))
# nusc.render_annotation(ann_token, out_path="./visibility.png")

# 9 传感器
nusc.sensor

# 10 标定传感器
print(nusc.calibrated_sensor[0])

# 11 ego_pose  车辆的位置信息
nusc.ego_pose[0]  # 和sample_data 的数据一对一

# 12 log 日志信息
print("Number of `logs` in our loaded database: {}".format(len(nusc.log)))

# 13 map 地图信息
print("There are {} maps masks in the loaded dataset".format(len(nusc.map)))